package dubbo.server4.impl.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.jms.Destination;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.rpc.RpcContext;

import dubbo.server1.api.TestService1;
import dubbo.server2.api.TestService2;
import dubbo.server3.api.TestService3;
import dubbo.server4.api.TestService4;
import dubbo.server4.api.domain.dto.UserInfoDto;
import dubbo.server4.api.domain.dto.WebPager;
import dubbo.server4.api.domain.entity.User;
import dubbo.server4.impl.mq.Producer;

@RestController
public class HelloWordController extends BaseController{
	private Logger logger = Logger.getLogger(HelloWordController.class);
	@Autowired
	private TestService1 testService1;
	@Autowired
	private TestService2 testService2;
	@Autowired
	private TestService3 testService3;
	@Autowired
	private TestService4 testService4;
	@Autowired
	private Producer producer;
	/*
	 * http://localhost:8084/hello
	 */

	@RequestMapping("/hello")
	public List<String> greeting() {
		logger.info("hello4");
		List<String> list = new ArrayList<String>();
		list.add("liuche");
		list.add("刘彻");
		list.add("王林");
		return list;
	}

	@RequestMapping("/call")
	public List<String> call() {
		logger.info("call4");
		List<String> list = new ArrayList<String>();
		if (testService1 != null)
			list.add("Call Success:"+testService1.getServerName());
		if (testService2 != null)
			list.add("Call Success:"+testService2.getServerName());
		if (testService3 != null)
			list.add("Call Success:"+testService3.getServerName());
		return list;
	}
	/**
	 * 测试Dubbo异步调用以及线程变化。
	 * 结果是线程不变，仍然被阻塞。并不能达到协程的效果
	 * @return
	 */
	@RequestMapping("/callAsync")
	public List<String> callAsync() {
		logger.info("call4");
		List<String> list = new ArrayList<String>();
		if (testService1 != null){
			Thread cu1 = Thread.currentThread();
			System.out.println("位置1  当前线程 " + cu1.getId() + " 时间 " + new Date().toLocaleString());
			String ret=testService1.getServerName();
			System.out.println("立即返回的为null:"+ret);
			Future<String> pFuture = RpcContext.getContext().getFuture();
			Thread cu2 = Thread.currentThread();
			System.out.println("位置2  当前线程 " + cu2.getId() + " 时间 " + new Date().toLocaleString());
	        //如果Person已返回，直接拿到返回值，否则线程wait，等待Person返回后，线程会被notify唤醒。
	        try {
				ret = pFuture.get();
				Thread cu3 = Thread.currentThread();
				System.out.println("位置3  当前线程 " + cu3.getId() + " 时间 " + new Date().toLocaleString());
			} catch (Exception e) {
				e.printStackTrace();
			}
	        System.out.println("返回的有值:"+ret);
			list.add("Call Success:"+ret);
		}
			
		return list;
	}
	/**
	 * mybatis访问数据库测试
	 * @return
	 */
	@RequestMapping("/list")
	public WebPager<UserInfoDto> list() {
		WebPager<UserInfoDto> list = testService4.getUserDepartmentList(null, null, 1, 10);
		return list;
	}
	/**
	 * 读写分离测试
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/read")
	public List<Map<String, Object>> read() throws Exception {
		List<Map<String, Object>> list = testService4.VisitDBReadAndWriteSplite();
		return list;
	}
	/**
	 * 主从复制测试
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/slave")
	public String slave() throws Exception {
		String ret = testService4.VisiteMasterAndSlave();
		return ret;
	}
	@RequestMapping("/redis")
	public String redis() {
		return testService4.getValueFromRedis();
	}
	@RequestMapping("/mq")
	public String mq(){
		 Destination destination = new ActiveMQQueue("mytest.queue"); 
		//Destination destination = new ActiveMQTopic("mytest.queue");  
		String text = "Hello!MQ";
		producer.sendMessage(destination, text);
		return "mq has send!";
	}
}
